<template>
  <div>
    <el-descriptions class="margin-top" title="个人信息" :column="3" border>
      <template slot="extra">
        <el-button type="primary" size="small" @click="showModifyPassword">修改密码</el-button>
      </template>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-user"></i>
          账号
        </template>
        {{this.informationForm.account}}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-mobile-phone"></i>
          姓名
        </template>
        {{this.informationForm.name}}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-location-outline"></i>
          性别
        </template>
        {{this.informationForm.gender}}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-location-outline"></i>
          民族
        </template>
        {{this.informationForm.nation}}
      </el-descriptions-item>
    </el-descriptions>
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="旧密码" prop="oldPass">
          <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {modifyPassword} from '../../../api/dormitory_admin'

export default {
  data () {
    var checkOldPass = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('旧密码不能为空'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    var validatePass3 = (rule, value, callback) => {
      this.modifyForm = {
        username: this.informationForm.account,
        password: this.ruleForm.oldPass,
        newPassword: this.ruleForm.pass
      }
      console.log(this.modifyForm)
      modifyPassword(this.modifyForm).then(res => {
        if (res.status) {
          callback()
        } else {
          callback(new Error('旧密码错误!'))
        }
      })
    }
    return {
      informationForm: {
        name: '李师傅',
        account: 'k1234',
        gender: '男',
        nation: '汉族',
        password: '123'
      },
      ruleForm: {
        pass: '',
        checkPass: '',
        oldPass: ''
      },
      rules: {
        pass: [
          {validator: validatePass, trigger: 'blur'}
        ],
        checkPass: [
          {validator: validatePass2, trigger: 'blur'},
          {validator: validatePass3, trigger: 'blur'}
        ],
        oldPass: [
          {validator: checkOldPass, trigger: 'blur'}
        ]
      },
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },
  methods: {
    showModifyPassword () {
      this.dialogFormVisible = true
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        console.log(valid)
        if (valid) {
          this.dialogFormVisible = false
          localStorage.clear()
          this.$router.push('/login')
          this.$message({
            message: '修改成功',
            type: 'success'
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style>
</style>
